package com.guavause.concurrency;

import com.google.common.util.concurrent.*;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

/**
 * @author kxd
 * @date 2019/1/10 15:44
 * description:
 */
public class ListenableFutureInGuava {


    public void getListenableFutures() {
        ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
        ListenableFuture<Explosion> explosion = service.submit(new Callable<Explosion>() {
            @Override
            public Explosion call() {
                Explosion explosion1 = new Explosion();
                return explosion1.getExplosion();
            }
        });
//void addCallback(ListenableFuture<V> future, FutureCallback<? super V> callback, Executor executor)
        Futures.addCallback(explosion, new FutureCallback<Explosion>() {
            @Override
            public void onSuccess(@Nullable Explosion explosion) {
                System.out.println("success");
            }

            @Override
            public void onFailure(Throwable throwable) {
                System.out.println("error");

            }
        }, service);
    }

   /* public void useListenableFuture() {
        ListenableFuture<RowKey> rowKeyFuture = indexService.lookUp(query);
        AsyncFunction<RowKey, QueryResult> queryFuction = new AsyncFunction<RowKey, QueryResult>() {
            @Override
            public ListenableFuture<QueryResult> apply(RowKey rowKey) throws Exception {
                return null;
            }
        };
        ListenableFuture<QueryResult> queryFuture = Futures.transformAsync(rowKeyFuture, queryFuction, queryExecutor);
    }*/


}
